package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.OnlineCourseListReqPara;
import com.lst.entity.response.OnlineCourseListResPara;
import com.lst.model.OnlineCourse;
import com.lst.service.OnlineCourseService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: SearchCourseServlet
 * @Description: 课程搜索接口
 */
public class SearchCourseServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	 private OnlineCourseService onlineCourseService = ctx.getBean(OnlineCourseService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		OnlineCourseListResPara resPara = new OnlineCourseListResPara();

		try {
			OnlineCourseListReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){

				String searchwork = reqPara.getSearchwork();
				Integer page = Integer.valueOf(reqPara.getPage());
				Integer pagesize = Integer.valueOf(reqPara.getPagesize());
				
				//根据关键字查询动态列表
				Map<String, Object> reqMap = new HashMap<String, Object>();		
				
				reqMap.put("enabled", true);
				reqMap.put("isDeleted", false);
				reqMap.put("searchwork", searchwork);

				PageBounds pageBounds = new PageBounds(page,pagesize);

			    List<OnlineCourse> onlineCourseList = onlineCourseService.queryList3(reqMap, pageBounds);

				resPara.setFeaturedList(onlineCourseList);
			    resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

			logger.info("ClientEditServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end: " + DateUtil.calLastedTime(startDate));

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public OnlineCourseListReqPara getReqPara(HttpServletRequest request) {

		OnlineCourseListReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new OnlineCourseListReqPara());

			reqPara = (OnlineCourseListReqPara) baseRequest.clone();

			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");
			String searchwork = request.getParameter("searchwork");

			logger.info("reqPara page: " + page);
			logger.info("reqPara pagesize: " + pagesize);
			logger.info("reqPara searchwork: " + searchwork);

			reqPara.setPage(page);
			reqPara.setPagesize(pagesize);
			reqPara.setSearchwork(searchwork);

		} catch (Exception e) { 
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		OnlineCourseListReqPara registerReqPara = (OnlineCourseListReqPara) reqPara;
		OnlineCourseListResPara registerResPara = (OnlineCourseListResPara) resPara;

		   if (StringUtils.isEmpty(registerReqPara.getPage())){
			   registerResPara.setCode(CommCode.M_A000000);
			   registerResPara.setMessage(CommCode.M_P000009);
			} else if (StringUtils.isEmpty(registerReqPara.getPagesize())){
				registerResPara.setCode(CommCode.M_A000000);
				registerResPara.setMessage(CommCode.M_P000010);
			} else if(StringUtils.isEmpty(registerReqPara.getSearchwork())){
				registerResPara.setCode(CommCode.M_A000000);
				registerResPara.setMessage(CommCode.M_F000001);
			}else {
				flag = true;
			}

		return  flag;
	}

}
